package com.ciwong.epaper.modules.me.adapter;

import android.app.Activity;
import android.graphics.Color;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.util.EpaperJumpManager;
import com.ciwong.epaper.modules.me.bean.BookDeskBean;
import com.ciwong.epaper.modules.me.bean.BookmarkBean;
import com.ciwong.epaper.modules.me.bean.EpaperInfoGradeNameBean;
import com.ciwong.epaper.modules.me.bean.FavoritesBook;
import com.ciwong.epaper.modules.me.bean.SubjectBean;
import com.ciwong.epaper.modules.me.util.MeJumpManager;
import com.ciwong.epaper.modules.me.widget.MarginDecoration;
import com.ciwong.epaper.modules.msg.bean.Ad;
import com.ciwong.epaper.util.DoubleClickCheckUtils;
import com.ciwong.epaper.util.IntentValue;
import com.ciwong.epaper.util.RequestCode;
import com.ciwong.epaper.widget.JiaoCaiTongBuItemView;
import com.ciwong.epaper.widget.MyTopParousel;
import com.ciwong.epaper.widget.MyTopParouselViewPager;
import com.ciwong.epaper.widget.OnTopParouselListener;
import com.ciwong.epaper.widget.SearchItemView;
import com.ciwong.mobilelib.i.BaseQuickOnItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by keepon on 2018/8/22.
 */
public class BookDeskRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "BookDeskRecyclerViewAdapter";
    /**
     * 搜索类型
     */
    public static final int ITEM_TYPE_SEARCH = 0;
    /**
     * 发现
     */
    public static final int ITEM_TYPE_FIND = 1;
    /**
     * 教材同步
     */
    public static final int ITEM_TYPE_JIAOCAITONGBU = 2;
    /**
     * 课外拓展
     */
    public static final int ITEM_TYPE_KEWAITUOZHAN = 3;

    /**
     * 收藏列表
     */
    public static final int ITEM_TYPE_SHOUCANGLIEBIAO = 4;


    /**
     * 教材同步
     */
    List<SubjectBean> mSubjectBeanList = new ArrayList<>();
    /**
     * 课外拓展
     */
    List<BookmarkBean> mKeWaiTuoZhanList = new ArrayList<>();
    /**
     * 收藏列表
     */
    List<FavoritesBook> mfavoritesBookList = new ArrayList<>();


    BookDeskBean mBookDeskBean;
    List<EpaperInfoGradeNameBean> mEpaperInfoGradeNameBeans;
    private boolean mIsLocal;
    private int showFind;
    private Activity mContext;
    /**
     * 广告轮播
     */
    List<Ad> mAdList = new ArrayList<>();
    List<String> imageList = new ArrayList<>();
    private MyTopParousel topParousel;


    public BookDeskRecyclerViewAdapter(Activity context, BookDeskBean bokDeskBean, List<EpaperInfoGradeNameBean> epaperInfoGradeNameBeans) {
        mContext = context;
        mBookDeskBean = bokDeskBean;
        mEpaperInfoGradeNameBeans = epaperInfoGradeNameBeans;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_SEARCH) {
            return new SearchViewHolder(new SearchItemView(mContext));
        } else if (viewType == ITEM_TYPE_FIND) {
            return new SearchViewHolder(initHead());
        } else if (viewType == ITEM_TYPE_KEWAITUOZHAN) {
            View kewaiItemView = LayoutInflater.from(mContext).inflate(R.layout.layout_jiaocaitongbu_item, parent, false);
            return new BookMarkViewHolder(kewaiItemView);
        } else if (viewType == ITEM_TYPE_JIAOCAITONGBU) {
            return new SubjectViewHolder(new JiaoCaiTongBuItemView(mContext));
        } else {
            View shoucangItemView = LayoutInflater.from(mContext).inflate(R.layout.layout_shoucangliebiao_item, parent, false);
            return new FavoritesViewHolder(shoucangItemView);
        }


    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int itemViewType = getItemViewType(position);
        if (itemViewType == ITEM_TYPE_SEARCH) {
        } else if (itemViewType == ITEM_TYPE_FIND) {
            topParousel.processData(true);
        } else if (itemViewType == ITEM_TYPE_JIAOCAITONGBU) {
            SubjectViewHolder subjectViewHolder = (SubjectViewHolder) holder;
            subjectViewHolder.bindSubjectData(mSubjectBeanList, mIsLocal);
        } else if (itemViewType == ITEM_TYPE_KEWAITUOZHAN) {
            BookMarkViewHolder bookMarkViewHolder = (BookMarkViewHolder) holder;
            bookMarkViewHolder.bindBookMarkData(mKeWaiTuoZhanList, mIsLocal);
        } else {
            FavoritesViewHolder favoritesViewHolder = (FavoritesViewHolder) holder;
            favoritesViewHolder.bindFavoritesData(mfavoritesBookList, mIsLocal);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            //发现
            return ITEM_TYPE_SEARCH;
        } else if (position == 1) {
            return ITEM_TYPE_FIND;
        } else if (position == 2) {
            return ITEM_TYPE_JIAOCAITONGBU;
        } else if (position == 3) {
            return ITEM_TYPE_KEWAITUOZHAN;
        } else if (position == 4) {
            return ITEM_TYPE_SHOUCANGLIEBIAO;
        } else {
            return 0;
        }

    }

    /**
     * 初始化头部轮播
     */
    private View initHead() {
        if (topParousel == null) {
            topParousel = new MyTopParousel(mContext, imageList);
            topParousel.setClick(new OnTopParouselListener() {

                @Override
                public void avertRepeatOnClick(int position) {
                    if (position < 0 || position >= mAdList.size()) {
                        return;
                    }
                    Ad mAd = mAdList.get(position);
                    if (mAd != null) {
                        if (mAd.getLinkType() == 2) {
                            if (mAd.getLinkAddress() != null && !TextUtils.isEmpty(mAd.getLinkAddress().trim())) {
                                MeJumpManager.jumpToCommonH5Activity(mContext, R.string.go_back, mAd.getAdvertiseName(), mAd.getLinkAddress().trim());
                            }
                        } else if (mAd.getLinkType() == 1) {
                            if (mAd.getSalesType() == 0) {
                                //跳服务
//                                jumpToBookOrServerDetails(mAd.getServiceId());
                            } else {
                                FavoritesBook mEpaperInfo = new FavoritesBook();
                                mEpaperInfo.setPackageId(mAd.getProductId());
                                mEpaperInfo.setProductId(mAd.getProductId());
                                mEpaperInfo.setServiceName(mAd.getServiceName());
                                mEpaperInfo.setServiceId(mAd.getServiceId());
                                mEpaperInfo.setIsFree(0);
                                mEpaperInfo.setIsPwd(0);
                                mEpaperInfo.setIstop(0);
                                //跳书籍
                                EpaperJumpManager.jumpToCatalog2(R.string.go_back, mContext, RequestCode.TeaWorkContent.HOME_WORK, mEpaperInfo,
                                        IntentValue.JumpToBookCatalogType.FROM_DESK_BOOK, -1, mAd.getServiceId());
                            }
                        }
                    }
                }

                @Override
                public void onShuffling(int position) {
                    // 上报广告
                }
            });
        } else {
            topParousel.setImageUrlList(imageList);
        }
        return topParousel.initView(true);
    }

    public int getCount(List<SubjectBean> subjectBeanList, List<BookmarkBean> bookmarkBeanList, List<FavoritesBook> favoritesBookList, List<Ad> ads, int showFind) {
        int count = 1;
        //广告没数据隐藏起来
        if (ads != null && ads.size() > 0) {
            count++;
        }
        //课外拓展没数据隐藏起来
        if (bookmarkBeanList != null && bookmarkBeanList.size() > 0) {
            count++;
        }
        //教材同步没数据隐藏起来
        if (subjectBeanList != null && subjectBeanList.size() > 0) {
            count++;
        }
        //收藏没数据隐藏起来
        if (favoritesBookList != null && favoritesBookList.size() > 0) {
            count++;
        }
        return count;
    }

    @Override
    public int getItemCount() {
        int allEmpty = getCount(mSubjectBeanList, mKeWaiTuoZhanList, mfavoritesBookList, mAdList, showFind);
        return allEmpty;
    }

    public void updateData(BookDeskBean bookDeskBean, boolean isLocal) {
        mBookDeskBean = bookDeskBean;
        mIsLocal = isLocal;
        mSubjectBeanList.clear();
        mSubjectBeanList.addAll(bookDeskBean.getSubject());
        mKeWaiTuoZhanList.clear();
        mKeWaiTuoZhanList.addAll(bookDeskBean.getBookmark());
        mfavoritesBookList.clear();
        mfavoritesBookList.addAll(bookDeskBean.getFavoritesBooks());
        showFind = bookDeskBean.getShowFind();
        imageList.clear();
        mAdList.clear();
        if (bookDeskBean.getAds() != null) {
            mAdList.addAll(bookDeskBean.getAds());
        }
        for (int i = 0; i < mAdList.size(); i++) {
            imageList.add(mAdList.get(i).getPhoto());
        }
        notifyDataSetChanged();
    }

    /**
     * 搜索适配器
     */
    public class SearchViewHolder extends RecyclerView.ViewHolder {

        public SearchViewHolder(View itemView) {
            super(itemView);
        }
    }


    /**
     * 同步教材适配器
     */
    public class SubjectViewHolder extends RecyclerView.ViewHolder {

        public SubjectViewHolder(View itemView) {
            super(itemView);
        }


        public void bindSubjectData(List<SubjectBean> subjectBeanList, boolean isLocal) {
            if (itemView instanceof JiaoCaiTongBuItemView) {
                JiaoCaiTongBuItemView jiaoCaiTongBuItemView = (JiaoCaiTongBuItemView) itemView;
                jiaoCaiTongBuItemView.bindData(mBookDeskBean, subjectBeanList, isLocal, mEpaperInfoGradeNameBeans);
            }

        }
    }


    /**
     * 课外拓展适配器
     */
    public class BookMarkViewHolder extends RecyclerView.ViewHolder {
        List<BookmarkBean> mBookmarkBeanList;
        TextView mTvTypeName;
        LinearLayout mLlMore;
        RecyclerView mExpansionRecyclerView;
        NewBookDeskExpansionAdapter mNewBookDeskExpansionAdapter;

        public BookMarkViewHolder(View itemView) {
            super(itemView);
            mTvTypeName = itemView.findViewById(R.id.tv_type_name);
            mExpansionRecyclerView = itemView.findViewById(R.id.recycler_view);
            mLlMore = itemView.findViewById(R.id.ll_more_menu);
            mLlMore.setVisibility(View.GONE);
            mExpansionRecyclerView.setFocusable(false);
            mTvTypeName.setText("课外拓展");
            mExpansionRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 4) {
                @Override
                public boolean canScrollHorizontally() {
                    return false;
                }

                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            });
            mBookmarkBeanList = new ArrayList<>();
            mNewBookDeskExpansionAdapter = new NewBookDeskExpansionAdapter(mContext, mBookmarkBeanList);
            mExpansionRecyclerView.setAdapter(mNewBookDeskExpansionAdapter);
            mNewBookDeskExpansionAdapter.setOnItemClickListener(new NewBookDeskExpansionAdapter.OnItemClickListener<BookmarkBean>() {
                @Override
                public void onItemClick(int positon, BookmarkBean bookmarkBean) {
                    if (DoubleClickCheckUtils.vertifyDuration()) {
                        if (bookmarkBean.getBookmarkId() == 12) {
                            MeJumpManager.jumpToFindH5Activity(mContext, R.string.go_back, mContext.getString(R.string.bookdesk_word_see));
                        } else {
                            MeJumpManager.jumpToBookTagActivity(R.string.go_back, bookmarkBean.getBookmarkId(), bookmarkBean.getBookmarkName(), (Activity) mContext);
                        }
                    }
                }
            });
        }


        public void bindBookMarkData(List<BookmarkBean> bookmarkBeanList, boolean isLocal) {
            if (bookmarkBeanList != null) {
                if (bookmarkBeanList.size() == 1) {
                    mExpansionRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 1) {
                        @Override
                        public boolean canScrollHorizontally() {
                            return false;
                        }

                        @Override
                        public boolean canScrollVertically() {
                            return false;
                        }
                    });
                } else if (bookmarkBeanList.size() == 2) {
                    mExpansionRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 2) {
                        @Override
                        public boolean canScrollHorizontally() {
                            return false;
                        }

                        @Override
                        public boolean canScrollVertically() {
                            return false;
                        }
                    });
                } else if (bookmarkBeanList.size() == 3) {
                    mExpansionRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3) {
                        @Override
                        public boolean canScrollHorizontally() {
                            return false;
                        }

                        @Override
                        public boolean canScrollVertically() {
                            return false;
                        }
                    });
                } else {
                    mExpansionRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 4) {
                        @Override
                        public boolean canScrollHorizontally() {
                            return false;
                        }

                        @Override
                        public boolean canScrollVertically() {
                            return false;
                        }
                    });
                }
            }
            mBookmarkBeanList = bookmarkBeanList;
            mNewBookDeskExpansionAdapter.updateList(mBookmarkBeanList);

        }
    }

    /**
     * 收藏列表适配器
     */
    public class FavoritesViewHolder extends RecyclerView.ViewHolder {
        private List<FavoritesBook> mfavoritesBookList;
        TextView mTvTypeName;
        LinearLayout mLlMore;
        RecyclerView mExpansionRecyclerView;
        FavoritesListAdapter favoritesListAdapter;
        TextView go_search_text;

        public FavoritesViewHolder(View itemView) {
            super(itemView);
            mTvTypeName = itemView.findViewById(R.id.tv_type_name);
            mExpansionRecyclerView = itemView.findViewById(R.id.recycler_view);
            mLlMore = itemView.findViewById(R.id.ll_more_menu);
            go_search_text = itemView.findViewById(R.id.go_search_text);
            mLlMore.setVisibility(View.GONE);
            mTvTypeName.setText("收藏列表");
            mExpansionRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
            mExpansionRecyclerView.addItemDecoration(new MarginDecoration(mContext));
            mfavoritesBookList = new ArrayList<>();
            favoritesListAdapter = new FavoritesListAdapter(R.layout.activity_favorites_list_item, mfavoritesBookList);
            mExpansionRecyclerView.setAdapter(favoritesListAdapter);

            if (null != mfavoritesBookList && mfavoritesBookList.size() > 0) {
                go_search_text.setVisibility(View.GONE);
            } else {
                go_search_text.setVisibility(View.VISIBLE);
                setSearchText();
                go_search_text.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        EpaperJumpManager.jumpToSerachBook(R.id.go_back, (Activity) mContext, RequestCode.TeaWorkContent.REQ_CODE_TO_CHOOSE_WORD);
                    }
                });
            }
            favoritesListAdapter.setOnItemClickListener(new BaseQuickOnItemClickListener() {
                @Override
                public void avertRepeatOnClick(BaseQuickAdapter adapter, View view, int position) {
                    try {
                        FavoritesBook favoritesBook = mfavoritesBookList.get(position);
                        if (null != favoritesBook) {
                            EpaperJumpManager.jumpToCatalog2(R.string.go_back, (Activity) mContext, RequestCode.TeaWorkContent.HOME_WORK, favoritesBook, IntentValue.JumpToBookCatalogType.FROM_BOOK_DESK, -1, favoritesBook.getServiceId());
                        }
                    } catch (Exception e) {

                    }
                }
            });

        }

        public void setSearchText() {
            if (null == mBookDeskBean.getFavoritesBooks() || mBookDeskBean.getFavoritesBooks().size() == 0) {
                SpannableStringBuilder searchText = new SpannableStringBuilder("暂无收藏书籍 去搜索");
                ForegroundColorSpan GreenSpan = new ForegroundColorSpan(Color.parseColor("#5ce191"));
                searchText.setSpan(GreenSpan, 7, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                go_search_text.setText(searchText);
                go_search_text.setVisibility(View.VISIBLE);
            } else {
                go_search_text.setVisibility(View.GONE);
            }
        }

        public void bindFavoritesData(List<FavoritesBook> favoritesBooks, boolean isLocal) {
            if (favoritesBooks != null && favoritesBooks.size() > 0) {
                go_search_text.setVisibility(View.GONE);
                /*if (favoritesBooks.size() == 1) {
                    mExpansionRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 1) {
                        @Override
                        public boolean canScrollHorizontally() {
                            return false;
                        }

                        @Override
                        public boolean canScrollVertically() {
                            return false;
                        }
                    });
                } else if (favoritesBooks.size() == 2) {
                    mExpansionRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 2) {
                        @Override
                        public boolean canScrollHorizontally() {
                            return false;
                        }

                        @Override
                        public boolean canScrollVertically() {
                            return false;
                        }
                    });
                } else {*/
                mExpansionRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3) {
                    @Override
                    public boolean canScrollHorizontally() {
                        return false;
                    }

                    @Override
                    public boolean canScrollVertically() {
                        return false;
                    }
                });
            } else {
                go_search_text.setVisibility(View.VISIBLE);
                setSearchText();
                go_search_text.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        EpaperJumpManager.jumpToSerachBook(R.id.go_back, (Activity) mContext, RequestCode.TeaWorkContent.REQ_CODE_TO_CHOOSE_WORD);
                    }
                });
            }

            mfavoritesBookList = favoritesBooks;
            favoritesListAdapter.setNewData(mfavoritesBookList);
        }
    }
}
